﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;

namespace Toolkit.Web.Mvc.Validation
{
    public class PostalCodeModelValidator : DataAnnotationsModelValidator<PostalCodeAttribute>
    {
        public PostalCodeModelValidator(ModelMetadata metadata, ControllerContext controllerContext, PostalCodeAttribute attribute)
            : base(metadata, controllerContext, attribute)
        {
        }

        public override IEnumerable<ModelClientValidationRule> GetClientValidationRules()
        {
            ModelClientValidationRule rule = new ModelClientValidationRule
            {
                ValidationType = "postalCode",
                ErrorMessage = Attribute.FormatErrorMessage(Metadata.PropertyName),
            };

            rule.ValidationParameters.Add("dependentPropertyName", Attribute.IsoCountryCode2);

            return new[] { rule };
        }
    }

    public class PostalCodeDependentModelValidator : DependentFieldModelValidator<PostalCodeDependentAttribute>
    {
        public PostalCodeDependentModelValidator(ModelMetadata metadata, ControllerContext controllerContext, PostalCodeDependentAttribute attribute)
            : base(metadata, controllerContext, attribute)
        {
        }

        public override IEnumerable<ModelClientValidationRule> GetClientValidationRules()
        {
            ModelClientValidationRule rule = new ModelClientValidationRule
            {
                ValidationType = "postalCode",
                ErrorMessage = Attribute.FormatErrorMessage(Metadata.PropertyName),
            };

            rule.ValidationParameters.Add("dependentPropertyName", Attribute.DependentPropertyName);

            return new[] { rule };
        }
    }
}
